#ifndef CGUI_H
#define CGUI_H

#include <QAction>
#include <QCloseEvent>
#include <QIcon>
#include <QList>
#include <QMainWindow>
#include <QMap>
#include <QModelIndex>
#include <QSettings>
#include <QTableView>
#include <QTreeView>

#include "src/ctreemodel.h"
#include "src/cdatabase.h"
#include "src/centriesmodel.h"
#include "src/caddsectiondialog.h"
#include "src/caddfielddialog.h"
#include "src/caddentrydialog.h"
#include "src/coutputgenerator.h"

#include "src/ctextbrowser.h"
#include "src/ctableview.h"
#include "src/ctreeview.h"
#include "src/constants.h"
#include "src/cstartdialog.h"
#include "src/creplicator.h"

#ifdef IS_MOBILE
    #include "src/mobile/stackedmainwindow.h"
#else
    #include "src/desktop/mainwindow.h"
#endif // IS_MOBILE

typedef QMap<int, QAction*> ActionMap;

class CGui : public QObject
{
    Q_OBJECT

    enum
    {
        START_OF_TREEACTIONS,

        AddSubsection,
        EditSubsection,
        DeleteSubsection,

        END_OF_TREEACTIONS
    };

    enum
    {
        START_OF_TABLEACTIONS = END_OF_TREEACTIONS,

        AddEntry,
        EditEntry,
        DeleteEntry,

        END_OF_TABLEACTIONS
    };

    enum
    {
        START_OF_ENTRYACTIONS = END_OF_TABLEACTIONS,

        AddField,

        END_OF_ENTRYACTIONS
    };

    enum
    {
        START_OF_COMMONACTIONS = END_OF_ENTRYACTIONS,

        ShowHide,
        Synchronize,
        ShowWizard,
        ShowAbout,
        Exit,

        END_OF_COMMONACTIONS
    };

    ActionMap m_treeActions;
    ActionMap m_tableActions;
    ActionMap m_entryActions;
    ActionMap m_commonActions;

    CTreeModel *m_model;
    CEntriesModel *m_entries;
    CDatabase *m_db;
    COutputGenerator *m_outGen;

    CEntry *m_current;
    QSettings *m_set;

    CReplicator *m_replicator;

// GUI classes
    CTextBrowser *m_textBrowser;
    CTreeView *m_treeView;
    CTableView *m_tableView;

#ifdef IS_MOBILE
    StackedMainWindow *m_wnd;
#else
    MainWindow* m_wnd;
#endif // IS_MOBILE

    void debug(QString msg) { qDebug() << "[Gui]\t\t" + msg; }

public:
    CGui(QSettings*);
    ~CGui();

    void start();

public slots:
    void show();
    void loadDatabase(QString filename);
    void createDatabase(QString filename, QString cipher);

private:
    void createActions();
    ActionMap append(QList<ActionMap*>);
    ActionMap append(ActionMap*, ActionMap*);
    QString m_cipher;
    bool m_alreadyLoaded;

//    void resizeEvent(QResizeEvent*) { resizeColumns(); }

    QString askKey();
    QWidget* getWindow();

private slots:
    void exit();
    void addSubsection();
    void editSubsection();
    void deleteSubsection();

    void addEntry();
    void editEntry();
    void deleteEntry();

    void addField();
    void editField(int);
    void deleteField(int);

    void saveImage(const QPixmap&);
    void saveFile(int);

    void showHidePassword();
    void resizeColumns();

    void showTreeContextMenu(const QPoint &);
    void showTableContextMenu(const QPoint &);
    void showBrowserContextMenu(const QPoint &);

    void loadSubsection(const QModelIndex&);
    void loadEntry(const QModelIndex&);
    void loadEntry(QString);
    void loadEntry();

    void setCurrentEntry(const QModelIndex&);

    void update(bool);
    void showWizard();
    void showSync();
    void showAbout();

    void syncFinished(int);

    void critical(QString caption, QString text);
    void close();
};

#endif // CGUI_H
